package cn.com.inossem.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import cn.com.inossem.db.DBUtils;

public class LogoMain extends Activity {

	private ViewPager mPager;
	// 包裹滑动图片的LinearLayout
	private ViewGroup viewPics;
	// 包裹小圆点的LinearLayout
	private LinearLayout viewPoints;
	// 将小圆点的图片用数组表示
	private ImageView[] imageViews;
	private ImageView imageView;
	GuidePageAdapter GuidePageAdapter;
	FrameLayout flview;
	private int count = 0;
	private SharedPreferences preferences;

	int positionid = 0;

	TelephonyManager tm;
	private List<View> views;

	// @Override
	// protected void onStart() {
	// super.onStart();
	// // 初始化configuration
	// tm = (TelephonyManager) this
	// .getSystemService(Context.TELEPHONY_SERVICE);
	// String tmDevice = tm.getDeviceId();
	// Configuration.cfg_map.put("devid", tmDevice);
	// }

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.logomain);
		DBUtils.copyDB(this, R.raw.sapdb);

//		copyIMImage();
		
		viewPoints = (LinearLayout) this.findViewById(R.id.viewGroup);
		// 添加小圆点的图片
		imageViews = new ImageView[4];
		for (int i = 0; i < 4; i++) {
			imageView = new ImageView(this);
			// 设置小圆点imageview的参数
			imageView.setLayoutParams(new LayoutParams(17, 17));// 创建一个宽高均为20
																// 的布局
			imageView.setPadding(20, 0, 20, 0);
			// 将小圆点layout添加到数组中
			imageViews[i] = imageView;

			// 默认选中的是第一张图片，此时第一个小圆点是选中状态，其他不是
			if (i == 0) {
				imageViews[i].setBackgroundResource(R.drawable.lan);
			} else {
				imageViews[i].setBackgroundResource(R.drawable.hui);
			}
			// 将imageviews添加到小圆点视图组
			viewPoints.addView(imageViews[i]);
		}

		preferences = getSharedPreferences("count", MODE_PRIVATE);
		count = preferences.getInt("count", 0);
		Log.v("info", "count: " + count);
		if (count == 0) {
			viewPoints.setVisibility(View.VISIBLE);
			flview = (FrameLayout) this.findViewById(R.id.flview);
			flview.setVisibility(ViewGroup.GONE);
			views = new ArrayList<View>();
			LayoutInflater inflater = getLayoutInflater();
			View view1 = inflater.inflate(R.layout.logo_layout, null);
			View view2 = inflater.inflate(R.layout.logo_layout, null);
			View view3 = inflater.inflate(R.layout.logo_layout, null);
			View view4 = inflater.inflate(R.layout.logo_next, null);
			views.add(view1);
			views.add(view2);
			views.add(view3);
			views.add(view4);

			mPager = (ViewPager) this.findViewById(R.id.guidePages);
			mPager.setAdapter(GuidePageAdapter = new GuidePageAdapter(views));
			mPager.setOnPageChangeListener(new GuidePageChangeListener());
			mPager.setOffscreenPageLimit(1);
			// Button gonext = (Button) this.findViewById(R.id.gonext);
			// gonext.setOnClickListener(new OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// // TODO Auto-generated method stub
			// if (positionid == 2) {
			// Intent intent = new Intent(LogoMain.this,
			// MainActivity.class);
			// startActivity(intent);
			// finish();
			// }
			// }
			// });

		} else {
			viewPoints.setVisibility(View.GONE);
		}

		new Handler().postDelayed(new Runnable() {
			public void run() {
				// execute the task
				// Intent intent = new Intent(LogoMain.this,MainActivity.class);
				// startActivity(intent);
				if (count == 0) {
					Editor editor = preferences.edit();
					editor.putInt("count", ++count);
					editor.commit();
					flview.setVisibility(ViewGroup.VISIBLE);
				} else {
					Intent intent = new Intent(LogoMain.this,
							MainActivity.class);
					startActivity(intent);
					finish();
				}
			}
		}, 2000);
	}
	
	private void copyIMImage() {
		File baseFile = new File(Environment.getExternalStorageDirectory(), "share");  
        if (!baseFile.exists()) {  
            baseFile.mkdir();  
        }
		File janFile = new File(baseFile.getAbsolutePath() + "/custom_msg_jan.png");
		if (!janFile.exists()) {
			try {
				janFile.createNewFile();  
				Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.custom_msg_jan);
				FileOutputStream fos = new FileOutputStream(janFile);  
				pic.compress(CompressFormat.PNG, 100, fos);  
				fos.flush();  
				fos.close();
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
		File kenFile = new File(baseFile.getAbsolutePath() + "/custom_msg_ken.png");
		if (!kenFile.exists()) {
			try {
				kenFile.createNewFile();  
				Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.custom_msg_ken);
				FileOutputStream fos = new FileOutputStream(kenFile);  
				pic.compress(CompressFormat.PNG, 100, fos);  
				fos.flush();  
				fos.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		File poFile = new File(baseFile.getAbsolutePath() + "/custom_msg_pon.png");
		if (!poFile.exists()) {
			try {
				poFile.createNewFile();  
				Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.custom_msg_pon);
				FileOutputStream fos = new FileOutputStream(poFile);  
				pic.compress(CompressFormat.PNG, 100, fos);  
				fos.flush();  
				fos.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	class GuidePageAdapter extends PagerAdapter {
		Context context;
		private List<View> views;

		public GuidePageAdapter(List<View> views) {
			this.views = views;
		}

		// 销毁position位置的界面
		@Override
		public void destroyItem(View v, int position, Object arg2) {
			// TODO Auto-generated method stub
			((ViewPager) v).removeView(views.get(position));

		}

		@Override
		public void finishUpdate(View arg0) {
			// TODO Auto-generated method stub

		}

		// 获取当前窗体界面数
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 4;
			// return 0;
		}

		// 初始化position位置的界面
		@Override
		public Object instantiateItem(View v, int position) {
			// TODO Auto-generated method stub
			View view = views.get(position);
			switch (position) {
			case 0:
				ImageView imageview = (ImageView) view
						.findViewById(R.id.imageview);
				imageview.setBackgroundResource(R.drawable.logoimage1);
				TextView text1 = (TextView) view.findViewById(R.id.text1);
				text1.setText("项目需求对接");
				TextView text2 = (TextView) view.findViewById(R.id.text2);
				text2.setText("自动智能检索，快速定位适合自己的项目需求");
				break;
			case 1:
				imageview = (ImageView) view.findViewById(R.id.imageview);
				imageview.setBackgroundResource(R.drawable.logoimage2);
				text1 = (TextView) view.findViewById(R.id.text1);
				text1.setText("更加贴合顾问的好友管理");
				text2 = (TextView) view.findViewById(R.id.text2);
				text2.setText("快速将失散多年的兄弟姐妹凝聚在圈子里");
				break;
			case 2:
				imageview = (ImageView) view.findViewById(R.id.imageview);
				imageview.setBackgroundResource(R.drawable.logoimage3);
				text1 = (TextView) view.findViewById(R.id.text1);
				text1.setText("个人信息管理");
				text2 = (TextView) view.findViewById(R.id.text2);
				text2.setText("快速创建个人简历，一键分享");
				break;
			case 3:
				Button next = (Button) view.findViewById(R.id.next);
				next.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(LogoMain.this,
								MainActivity.class);
						startActivity(intent);
						finish();
					}
				});
				break;
			}

			((ViewPager) v).addView(view);
			// ((ViewPager) v).addView(view);
			return view;
		}

		// 判断是否由对象生成界面
		@Override
		public boolean isViewFromObject(View v, Object arg1) {
			// TODO Auto-generated method stub
			return v == arg1;
		}

		@Override
		public void startUpdate(View arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public int getItemPosition(Object object) {
			// TODO Auto-generated method stub
			return super.getItemPosition(object);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public Parcelable saveState() {
			// TODO Auto-generated method stub
			return null;
		}
	}

	class GuidePageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int position) {
			// TODO Auto-generated method stub
			positionid = position;
			for (int i = 0; i < imageViews.length; i++) {
				imageViews[position].setBackgroundResource(R.drawable.lan);
				// 不是当前选中的page，其小圆点设置为未选中的状态
				if (position != i) {
					imageViews[i].setBackgroundResource(R.drawable.hui);
				}
			}

		}
	}

	private void showTips() {
		AlertDialog alertDialog = new AlertDialog.Builder(LogoMain.this)
				.setTitle("退出程序").setMessage("是否退出程序")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						LogoMain.this.finish();
						System.exit(0);
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						return;
					}
				}).create(); // 创建对话框
		alertDialog.show(); // 显示对话框
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			this.showTips();
			return false;
		}
		return false;
	}
}
